﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{

    private String omniaUser;
    
    /// <summary>
    /// gets the user current logged in
    /// </summary>
    private void getUser()
    {
        try
        {
            omniaUser = User.Identity.Name.Substring(User.Identity.Name.IndexOf('\\') + 1);
        }
        catch (Exception ex)
        {
            OmniaLogger.Log.Error("Problem getting loginname: ", ex);
        }
    }

   
    /// <summary>
    /// 
    /// </summary>
    /// <param name="service"></param>
    /// <param name="serviceSsoUrl"></param>
    private void jumpToService(string service, string serviceSsoUrl)
    {
        OmniaLogger.Log.Debug("Starting " + service + " site for user " + User.Identity.Name + " from ipaddress " + Request.UserHostAddress);
        Response.Write("<script>var newTab = window.open('about:blank', '_blank'); newTab.location = '");
        Response.Write(serviceSsoUrl);
        Response.Write("';</script>");
    }

    /// <summary>
    /// Get the Single Sign On url for SOM and opens a new browser window
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ButtonSom_Click(object sender, EventArgs e)
    {
        string somSsoUrl;
        SomSSO somSSO = new SomSSO();
        if (!String.IsNullOrEmpty(somSsoUrl = somSSO.getSomSSOUrl("Dewal")))
            jumpToService("SOM", somSsoUrl);
    }

    /// <summary>
    /// Get the Single Sign On url for Prowise and opens a new browser window
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ButtonProwise_Click(object sender, EventArgs e)
    {
        string prowiseSsoUrl;
        ProwiseSSO prowise = new ProwiseSSO();
        if (!String.IsNullOrEmpty(prowiseSsoUrl = prowise.getProwiseSSOUrl("Dewal", "demo@dewal-ict.nl")))
            jumpToService("Prowise", prowiseSsoUrl);            
    }

    /// <summary>
    /// Logs the user logged in to Omnia 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        getUser();
        OmniaLogger.Log.Debug("Omnia is started for user " + omniaUser + " from ipaddress " + Request.UserHostAddress);
        loginName.Text = omniaUser;
    }

}
